home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include <X11/Xlib.h>
- #include <X11/Xutil.h>
- #include <GL/glx.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <X11/keysym.h>
-
- static int RGBattributes_DB[] = {
- GLX_RGBA,
- GLX_RED_SIZE, 1,
- GLX_GREEN_SIZE, 1,
- GLX_BLUE_SIZE, 1,
- GLX_DOUBLEBUFFER,
- None,
- };
-
- static int CIattributes_DB[] = {
- GLX_DOUBLEBUFFER,
- None,
- };
-
- enum {
- BLACK = 0,
- RED,
- GREEN,
- YELLOW,
- BLUE,
- MAGENTA,
- CYAN,
- WHITE
- };
-
- #define COLOR_OFFSET_1 16
- #define COLOR_OFFSET_2 32
-
-
- static float rgbMap[][3] = {
- {0, 0, 0},
- {1, 0, 0},
- {0, 1, 0},
- {1, 1, 0},
- {0, 0, 1},
- {1, 0, 1},
- {0, 1, 1},
- {1, 1, 1}
- };
-
- static long W = 300, H = 300;
- static Display *dpy;
- static Window window;
- static Colormap cmap;
- int drawX = 1;
- int drawGL = 1;
- int doubleBuf = 1;
- int swaps = 1;
- GLenum drawBuffer = GL_FRONT;
- static long rgb;
- static long ci1 = BLUE, ci2 = GREEN;
-
-
-
- static GLuint LoadGLFont(void)
- {
- XFontStruct *fontInfo;
- Font id;
- GLuint base;
- int first, last;
-
- static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
-
- fontInfo = XLoadQueryFont(dpy, pattern1);
-
- id = fontInfo->fid;
- first = (int)fontInfo->min_char_or_byte2;
- last = (int)fontInfo->max_char_or_byte2;
-
-
- base = glGenLists(last+1);
- if (base == 0) {
- return 0;
- }
- glXUseXFont(id, first, last-first+1, (int)(base+first));
- return base;
- }
-
-
- static void Usage(void)
- {
- printf("Usage: xswap [-c]\n");
- printf(" -c: Run in color index mode\n");
- exit(-1);
- }
-
- static GLint DrawXFont(GLint x, GLint y, char *string, GLint len)
- {
- static char pattern1[] = "-adobe-courier-bold-o-normal--18-*-*-*-*-*-*-*";
- static char pattern2[] = "-*-*-*-*-*-*-18-*-*-*-*-*-*-*";
- static char size[] = "456781";
- GC gc;
- XGCValues values;
- XFontStruct *fontInfo;
- char **fontList;
- XColor exact, green;
- unsigned long mask;
- int count, i;
-
- glXWaitGL();
-
- fontList = XListFonts(dpy, pattern1, 1, &count);
- if (count == 0) {
- for (i = 0; i < 6; i++) {
- pattern2[14] = size[i];
- fontList = XListFonts(dpy, pattern2, 1, &count);
- if (count > 0) {
- break;
- }
- }
- }
- if (count == 0) {
- return GL_FALSE;
- }
-
- fontInfo = XLoadQueryFont(dpy, fontList[0]);
- if (fontInfo == NULL) {
- return GL_FALSE;
- }
-
- mask = GCForeground | GCFont;
- values.font = fontInfo->fid;
-
- if (rgb) {
- XAllocNamedColor(dpy, cmap, "Green", &exact, &green);
- values.foreground = green.pixel;
- } else {
- values.foreground = 2;
- }
-
- gc = XCreateGC(dpy, window, mask, &values);
- XDrawString(dpy, window, gc, (int)x, (int)y, string, (int)len);
-
- XFreeFontNames(fontList);
- glXWaitX();
- return GL_TRUE;
- }
-
- static void DoDisplay(void)
- {
- glLoadIdentity();
- glOrtho(0.0, W, 0.0, H, -0.5, 1000.0);
-
- if (swaps) {
- glDrawBuffer(GL_BACK);
- glClearColor(0.0, 0.0, 1.0, 0.0);
- glClearIndex(2);
- glClear(GL_COLOR_BUFFER_BIT);
- glDrawBuffer(GL_FRONT);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearIndex(0);
- glClear(GL_COLOR_BUFFER_BIT);
- } else {
- glDrawBuffer(GL_BACK);
- glClearColor(0.0, 0.0, 0.0, 0.0);
- glClearIndex(0);
- glClear(GL_COLOR_BUFFER_BIT);
- glDrawBuffer(GL_FRONT);
- glClearColor(0.0, 0.0, 1.0, 0.0);
- glClearIndex(2);
- glClear(GL_COLOR_BUFFER_BIT);
- }
-
- glDrawBuffer(drawBuffer);
-
- if (drawGL) {
- glRasterPos2f(10.0, 10.0);
- glCallLists(17, GL_UNSIGNED_BYTE, (unsigned char *)"this is a GL font");
- glXWaitGL();
- }
- if (drawX) {
- DrawXFont(10,40, "this is an X font", 17);
- glXWaitX();
- }
-
- if (drawBuffer == GL_FRONT)
- printf("drawGL=%d, drawX=%d, drawbuffer=GL_FRONT\n",drawGL, drawX);
- else
- printf("drawGL=%d, drawX=%d, drawbuffer=GL_BACK\n",drawGL, drawX);
- glFlush();
- }
-
- static Bool WaitForMapNotify(Display *d, XEvent *e, char *arg)
- {
- if ((e->type == MapNotify) && (e->xmap.window == (Window)arg)) {
- return GL_TRUE;
- }
- return GL_FALSE;
- }
-
- int main(long argc, char** argv)
- {
- XVisualInfo *vi;
- XSetWindowAttributes swa;
- GLXContext cx;
- XEvent event;
- GLboolean needDisplay;
- int i;
- GLuint base;
-
- rgb = 1;
- doubleBuf = 1;
- for (i = 1; i < argc; i++) {
- if (argv[i][0] == '-') {
- switch (argv[i][1]) {
- case 'c':
- rgb = 0;
- break;
- default:
- Usage();
- }
- } else {
- Usage();
- }
- }
-
- dpy = XOpenDisplay(0);
- if (!dpy) {
- fprintf(stderr, "Can't connect to display \"%s\"\n", getenv("DISPLAY"));
- return -1;
- }
-
- vi = glXChooseVisual(dpy, DefaultScreen(dpy),
- (rgb ? RGBattributes_DB : CIattributes_DB));
- if (!vi) {
- fprintf(stderr, "No appropriate visual on \"%s\"\n",
- getenv("DISPLAY"));
- return -1;
- }
-
- cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual,
- rgb ? AllocNone : AllocAll);
- if (!rgb) {
- XColor buf;
- int i;
-
- buf.flags = DoRed | DoGreen | DoBlue;
-
- /* Init color map */
- for (i=0; i<16; i++) {
- buf.pixel = i;
- buf.blue = (i & 4) ? 65535 : 0;
- buf.green = (i & 2) ? 65535 : 0;
- buf.red = (i & 1) ? 65535 : 0;
- if (i > 8) {
- buf.red /= 2;
- buf.green /= 2;
- buf.blue /= 2;
- }
- XStoreColor(dpy, cmap, &buf);
- }
- }
-
-
- swa.border_pixel = 0;
- swa.colormap = cmap;
- swa.event_mask = ExposureMask | StructureNotifyMask | KeyPressMask
- | KeyReleaseMask;
- window = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 10, 10,
- W, H,
- 0, vi->depth, InputOutput, vi->visual,
- CWBorderPixel|CWColormap|CWEventMask, &swa);
- XSetWMColormapWindows(dpy, window, &window, 1);
- XMapWindow(dpy, window);
- XIfEvent(dpy, &event, WaitForMapNotify, (char*)window);
-
- cx = glXCreateContext(dpy, vi, 0, GL_TRUE);
- if (!glXMakeCurrent(dpy, window, cx)) {
- fprintf(stderr, "Can't make window current to context\n");
- return -1;
- }
-
- /****
- if (!rgb) {
- XColor xc[32+2];
-
- unsigned long pixel;
- unsigned short red, green, blue;
- char flags;
- char pad;
-
- for (i = 0; i < 16; i++) {
- xc[i].pixel = i + COLOR_OFFSET_1;
- xc[i].red = 0;
- xc[i].green = 0;
- xc[i].blue = (unsigned short) (65535.0 * (i / 15.0));
- xc[i].flags = DoRed | DoGreen | DoBlue;
-
- xc[i+16].pixel = i + COLOR_OFFSET_2;
- xc[i+16].red = 0;
- xc[i+16].green = (unsigned short) (65535.0 * (i / 15.0));
- xc[i+16].blue = 0;
- xc[i+16].flags = DoRed | DoGreen | DoBlue;
- }
-
- xc[32].pixel = BLUE;
- xc[32].red = xc[32].green = 0;
- xc[32].blue = 65535;
- xc[32].flags = DoRed|DoGreen|DoBlue;
-
- xc[32].pixel = GREEN;
- xc[32].red = xc[32].blue = 0;
- xc[32].green = 65535;
- xc[32].flags = DoRed|DoGreen|DoBlue;
- XStoreColors(dpy, cmap, xc, 34);
- xswap
- ****/
-
- base = LoadGLFont();
- glListBase(base);
-
- needDisplay = GL_TRUE;
- for (;;) {
- do {
- XNextEvent(dpy, &event);
- switch (event.type) {
- case Expose:
- needDisplay = GL_TRUE;
- break;
- case ConfigureNotify:
- W = event.xconfigure.width;
- H = event.xconfigure.height;
- needDisplay = GL_TRUE;
- break;
- case KeyPress:
- {
- char buf[100];
- int rv;
- KeySym ks;
-
- rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
- switch (ks) {
- case XK_f:
- drawBuffer = GL_FRONT;
- needDisplay = GL_TRUE;
- break;
- case XK_b:
- drawBuffer = GL_BACK;
- needDisplay = GL_TRUE;
- break;
- case XK_g:
- drawGL = !drawGL;
- needDisplay = GL_TRUE;
- break;
- case XK_x:
- drawX = !drawX;
- needDisplay = GL_TRUE;
- break;
- case XK_r:
- needDisplay = GL_TRUE;
- break;
- case XK_S:
- case XK_s:
- glXSwapBuffers(dpy, window);
- swaps = !swaps;
- break;
- case XK_Escape:
- return 0;
- }
- }
- break;
- }
- } while (XPending(dpy) != 0);
-
- if (needDisplay) {
- needDisplay = GL_FALSE;
- DoDisplay();
- }
- }
- }
-